var currentPage = 1;

//列表
loadMyEssay();

function loadMyEssay() {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#focusShopList").html("");

		$.ajax({
			url: urlcore + "/api/focusShop/selectPage?size="+5+"&current=" + pageNo,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {

						var goodsList = '';
						$.each(n.goodsList, function(i, m) {
							var coverUrl=m.coverUrl==null?'http://app.jiagangwangluo.com:8080/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':m.coverUrl; 
							coverUrl=isHasImg(coverUrl);
							var tradePrice=m.tradePrice==null?"0":m.tradePrice;
							var packPrice=m.packPrice==null?"0":m.packPrice;
							goodsList += '<dl>' +
								'<a href="2.2.0goods_detail.html?id='+m.id+'">' +
								'<dt>' +
								'<img class="goodsCoverImg" src="' + coverUrl + '" />' +
								'</dt>' +
								'<dd>&yen;' + tradePrice + '~' + packPrice + '</dd>' +
								'</a>'+ 
								'</dl>';

						});
						
						var headImg=n.shop.headImg==null?'http://app.jiagangwangluo.com:8080/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.shop.headImg;  
                        headImg=isHasImg(headImg);
                       var storeName=n.shop.storeName==null?"暂无":n.shop.storeName;
						var storeLevelName = '';
						if(n.shop.storeLevel == 1) {
							storeLevelName = '<img src="images/grade_tong.png" />&nbsp;<em>铜牌店铺</em>';
						} else if(n.shop.storeLevel == 2) {
							storeLevelName = '<img src="images/grade_yin.png" />&nbsp;<em>银牌店铺</em>';
						} else if(n.shop.storeLevel == 3) {
							storeLevelName = '<img src="images/grade_jin.png" />&nbsp;<em>金牌店铺</em>';
						}
						var creditLevelName = '';
						if(n.shop.creditLevel == 1) {
							creditLevelName = '<img src="images/aixiin.png" />';
						} else if(n.shop.creditLevel == 2) {
							creditLevelName = '<img src="images/aixiin.png" /><img src="images/aixiin.png" />';
						} else if(n.shop.creditLevel == 3) {
							creditLevelName = '<img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" />';
						} else if(n.shop.creditLevel == 4) {
							creditLevelName = '<img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" />';
						}else if(n.shop.creditLevel == 5) {
							creditLevelName = '<img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" /><img src="images/aixiin.png" />';
						}
						
						
						var thislist =
							'<div class="wode_shoucang_dianpu">' +
							'<div class="wode_dianpu_title clearfix">' +
							'<dl class="fl clearfix">' +
							'<dt>' +
							'<img class="storeNameImg" src="' + headImg + '" />' +
							'</dt>' +
							'<dd>' +
							'<h3>' + storeName + '</h3>' +
							'<div class="clearfix">' +
							'<span>店铺等级：</span>' + storeLevelName +
							'</div>' +
							'<div class="clearfix">' +
							'<span>信誉等级：</span>' + creditLevelName +
							'</div>' +
							'</dd>' +
							'</dl>' +
							'<div class="fr">' +
							'<a href="10.0.0dianpu.html?shopId='+n.shopId+'">进入店铺</a>' +
							'<span onclick="cancelFocus('+n.id+');" class="wode_quxiao Shoucang">取消关注</span>' +
							'</div>' +
							'</div>' +
							'<div class="wode_dianpu_goods clearfix">' + goodsList+
							'</div>' +
							'</div>';
						$('#focusShopList').append(thislist);
					});
					
					$(function(){
						var imgList = document.getElementsByClassName("goodsCoverImg");  
					    var w=165;
					    var h=165;
					    for(var i=0;i<imgList.length;i++){
					        if(imgList[i].width>w || imgList[i].height>h){  
					            imgList[i].width = w;  
					            imgList[i].heigth = h;
					        }  
					    }  
					    
					    var imgList1 = document.getElementsByClassName("storeNameImg");  
					    var w1=90;
					    var h1=90;
					    for(var i=0;i<imgList1.length;i++){
					        if(imgList1[i].width>w1 || imgList1[i].height>h1){  
					            imgList1[i].width = w1;  
					            imgList1[i].heigth = h1;
					        }  
					    }  
					})

					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					//alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}
//取消关注
function cancelFocus(id) {
	if(!layer.confirm("确认取消吗?")) {
		return;
	}
	$.ajax({
		url: urlcore + '/api/focusShop/update',
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"id": id,
			"status": 2
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
				layer.alert("取消成功");
				//跳转列表页
				window.location.href = "22.0.0wode_shoucang_dianpu.html";
			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href = "1.0.0denglu.html";
				}
			} else {
				layer.alert(data.msg);
			}

		},
		error: function() {
			layer.alert("error");
		}

	});
}